var windowWidth = wx.getSystemInfoSync().windowWidth;
var windowHeight = wx.getSystemInfoSync().windowHeight;
var keyHeight = 0;
var app = getApp();


// pages/chatRoom/chatRoom.js
Page({

    /**
     * 页面的初始数据
     */ 
    data: {
        task:{},
        openid:"",
        inputBottom:0,
        inputButton:"",
        token:wx.getStorageSync('token'),
        url:"",
        nickName:"",
        userUrl:wx.getStorageSync('UserAvatarUrl'),


        chatMsg:[
          {
            person:"yourside",
            Msg:"以上为打招呼消息",
            time:"2020"
        },


        ]
 

    }, 


    //获取消息输入框内的值并将信息添加到chatMsg中渲染并清空消息输入框内的信息
    sendClick: function(e) {
      var app =getApp()
      var that = this
      console.log(e.detail.value)
        that.data.chatMsg.push({
          person: 'myside',
          url:that.data.userUrl,
          Msg: e.detail.value,
          time:new Date().getTime(),
        }) 
    wx.setStorageSync(that.data.openid, that.data.chatMsg)
    app.sendSocketMessage({"type":"sendMsg","target":that.data.openid,"msg":that.data.inputButton})
        this.setData({
          chatMsg:that.data.chatMsg,
          inputVal:"",
        })

        console.log("消息框信息发送")
         },

    //设置绑定事件 消息输入框值改变时inputButton的值相应改变
    msgChange: function(e) {
      this.setData({
        inputButton:e.detail.value
      })
    },
    

    //获取inputButton的值添加到chatMSg中渲染并清空消息输入框信息
    sendMsg: function(){
      var app = getApp()
      var that =this
      that.data.chatMsg.push({
        person: 'myside',
        url:that.data.userUrl,
        Msg: that.data.inputButton,
        time:new Date().getTime(), 
      }) 
      wx.setStorageSync(that.data.openid, that.data.chatMsg)
      console.log(that.data.inputButton)
      app.sendSocketMessage({"type":"sendMsg","target":that.data.openid,"msg":that.data.inputButton})
      that.setData({
        chatMsg:that.data.chatMsg,
        inputButton:"",
        inputVal:"",
      })



      console.log("消息发送成功")
    },



      
  // 获取聚焦定位到最后一条信息
  focus: function(e) {
    keyHeight = e.detail.height;
    this.setData({
      toView: 'msg-' + ((this.data.chatMsg).length - 1),
      inputBottom: keyHeight + 'px'
    })


  },

  //失去聚焦(软键盘消失)
  blur: function(e) {
    this.setData({
      scrollHeight: '100vh',
      inputBottom: 0
    })
    this.setData({
      toView: 'msg-' + ((this.data.chatMsg).length - 1)
    })

  },




    onLoad: function (options) {
      var that =this

        ////通过用户openid获取用户nickname和头像url
        console.log(JSON.parse(options.openid))
        var openid = JSON.parse(options.openid)
        wx.request({
          // url: 'http://localhost:10001/getUserInfoById?openid='+options.openid,
          url:'http://119.23.252.15:10001/getUserInfoById?openid='+openid,
          success(res){
            console.log(res.data)
        that.setData({
          // userUrl:wx.getStorageSync('UserAvatarUrl'),
          // token:wx.getStorageSync('token'),
          // openid:options.openid
          openid:openid,
          url:res.data.data.userInfo.avatarUrl,
          nickName:res.data.data.userInfo.nickName,
          userUrl:wx.getStorageSync('UserAvatarUrl')
        })
          }
        })


      //通过openid获取缓存若无缓存则设置缓存
      if(wx.getStorageSync(openid)==""){
        that.setData({
          chatMsg:[{
                person:"yourside",
                Msg:"以上为打招呼消息",
                time:new Date().getTime()
                    
              },]})
              wx.setStorageSync(openid, that.data.chatMsg)


      }


      else{
        that.setData({
          chatMsg:wx.getStorageSync(that.data.openid)
        })
      }



      //设置定时更新聊天信息
      setInterval(res=>{
        if(that.data.chatMsg!==wx.getStorageSync(openid))
        that.setData({
          chatMsg:wx.getStorageSync(openid),
        })
      },1500)





    },



    //退出页面时更新msgListData将聊天信息chatMsg更新到msgListData中
    onUnload(){
      var that =this

      //获取聊天列表缓存若为空则设置缓存
      if (wx.getStorageSync('msgListData')==""){
        var msgListData=[]
      }
      else{
      var msgListData = wx.getStorageSync('msgListData')
      }
      // task.close(() => {console.log("关闭链接")})


      let flag=false
      //判断聊天列表内是否有与该用户聊天信息若有则更新若无则添加
      for (let i = 0; i < msgListData.length; i++) {


        if (msgListData[i].openid==that.data.openid){
        let y =that.data.chatMsg.length-1
        msgListData[i].recentmessage=that.data.chatMsg[y].Msg
        msgListData[i].time=that.data.chatMsg[y].time
        msgListData[i].url=that.data.url
        msgListData[i].nickname=that.data.nickName
        wx.setStorageSync('msgListData',msgListData)    
        flag=true
        console.log("修改信息成功")
        }
      }
      if(!flag){
        let y = that.data.chatMsg.length-1
        msgListData.push({
          openid:that.data.openid,
          url:that.data.url,
          nickname:that.data.nickName,
          recentmessage:that.data.chatMsg[y].Msg, 
          time:that.data.chatMsg[y].time
        },)
        console.log(msgListData)


        //更新消息列表缓存
        wx.setStorage({key:'msgListData',data: msgListData,success(){
          console.log("缓存消息成功")
        }})

      }



      //退出页面则保存聊天缓存
      wx.setStorage({key:that.data.openid,data:that.data.chatMsg,success(){
        console.log("聊天信息缓存成功")
        var Msg=wx.getStorageSync(that.data.openid)
        console.log(Msg)
      }
      })
      


    },




})

